package com.rao.rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rao.rabbitmq.util.ConnectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 生产者（工作消息模型演示）
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/6/17 22:35
 */
public class ProducerSend {
    private static final Logger log = LoggerFactory.getLogger(ProducerSend.class);

    private static final String QUEUE_NAME = "test_work_queue";

    public static void main(String[] argv) throws Exception {
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 从连接中创建通道，使用通道才能完成消息相关的操作
        Channel channel = connection.createChannel();
        // 声明（创建）队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 循环发布任务
        for (int i = 0; i < 50; i++) {
            // 消息内容
            String message = "task ... " + i;
            // 向指定的队列中发送消息
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            log.info("向MQ发送消息=【{}】", message);

            Thread.sleep(i * 2);
        }
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}
